//
//  CPMainTabBar.swift
//  weibo
//
//  Created by zcp on 16/5/10.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit

class CPMainTabBar: UITabBar {

    //闭包属性
    var callback: (()->())?
//    let abtn  = UIButton()
////    //在babbar中添加子控件
//    override init(frame: CGRect) {
//        super.init(frame: frame)
////        let btn = UIButton()
//        abtn.backgroundColor = UIColor.redColor()
//        self.addSubview(abtn)
//    }
////
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    //重新布局babbar的中控件
    override func layoutSubviews() {
        super.layoutSubviews()
        //每个控件平均宽度
        let width = self.frame.width/5.0
        let height = self.frame.height
        //记录第几个控件
        var index = 0
        //重新设置
        for view in self.subviews{
//        print("\(view)")
//            UITabBarButton
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!){
            view.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: height)
            index++
            }
            if index == 2
            {
                index++
            }
        }
        //按钮
        btn.frame = CGRect(x: width*2.0, y: 0, width: width, height: height)
        
    }
    //点击操作
    func btnClick(){
    
        //调用闭包
        callback?()
    }
    
    //懒加载按钮
    lazy var btn: UIButton = {
    
        let btn = UIButton()
//        btn.backgroundColor = UIColor.redColor()
        //设置图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //绑定点击事件
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(btn)
        return btn
    
    }()

}
